ParticleEngine = class()

function ParticleEngine:init()
    self.particles = {}
    self.t = Tesselator()
end

function ParticleEngine:add(particle)
    table.insert(self.particles,particle)
end

function ParticleEngine:tick()
    local remove = table.remove
    for k,v in pairs(self.particles) do
        v:tick()
        if v.removed then
            remove(self.particles,k)
        end
    end
end


function ParticleEngine:render(player,a)
    if #self.particles == 0 then return end
    
    local xa = -math.cos(player.yRot * math.pi / 180)
    local za = -math.sin(player.yRot * math.pi / 180)
    
    local xa2 = -za * math.sin(player.xRot * math.pi / 180)
    local za2 = xa * math.sin(player.xRot * math.pi / 180)
    
    local ya = math.cos(player.xRot * math.pi / 180)
    
    self.t:begin()
    self.t:bindTex(Tex)
    self.t:setShader(shader("Project:TextureColorShader"))
    for i,v in ipairs(self.particles) do
        if v then
            v:render(self.t, a, xa, ya, za, xa2, za2)
        end
    end
    self.t:End()
    self.t.mesh.shader.GlobalColor = world.globalColor
    self.t:render()
end